XBtnExp={
	["xpbonus"]=0,["tpbonus"]=0,
	["xpdept"]=0,["tpdept"]=0,
	["xpsession"]=0,["tpsession"]=0,
	["xpnew"]=0,["xpold"]=0,
	["tpnew"]=0,["tpold"]=0,
	["xplast"]=0,["tplast"]=0,
	["xprepeats"]=0,
	["xpleft"]=0,["xpmax"]=0,
};
 
function GetTotalPowerPoint(param)
  return GetTpExp(param);
end
function XBarExp_OnLoad(this)
	this:RegisterEvent("LOADING_END");
end

function XBarExp_OnEvent(this,event)
--	ChatFrame1:AddMessage("XBar-Exp : "..event);
	if (event=="LOADING_END") then
		this:RegisterEvent("TP_EXP_UPDATE");
		this:RegisterEvent("UNIT_CLASS_CHANGED");
		this:RegisterEvent("PLAYER_EXP_CHANGED");
	end
	local xpbonus=XBtnExp["xpbonus"];
	local tpbonus=XBtnExp["tpbonus"];
	XBtnExp["xpbonus"],XBtnExp["tpbonus"]=GetPlayerExtraPoint();
	if (tpbonus<XBtnExp["tpbonus"] or xpbonus<XBtnExp["xpbonus"]) then
		local info="U gained |cff00FF00";
		info=info..XBar_SetDecimals(XBtnExp["tpbonus"]-tpbonus);
		info=info.."|r TP and |cff00FF00";
		info=info..XBar_SetDecimals(XBtnExp["xpbonus"]-xpbonus);
		info=info.."|r Exp from housebonus.";
		DEFAULT_CHAT_FRAME:AddMessage(info,1,1,1);
	end
	XBtnExp["xpdebt"],XBtnExp["tpdebt"]=GetPlayerExpDebt();
	if (XBtnExp["xpdebt"]<0 or XBtnExp["tpdebt"]<0) then XBarExp_B:SetColor(1,0,0); XBarExp_F_Text:SetColor(1,.7,.7);
	else XBarExp_B:SetColor(1,1,1); XBarExp_F_Text:SetColor(1,1,1); end
	if XBtnExp["tpnew"]~=GetTpExp("Tp") then
		if XBtnExp["tpnew"]~=0 then XBtnExp["tpold"]=XBtnExp["tpnew"];
		else XBtnExp["tpold"]=GetTpExp("Tp"); end
		XBtnExp["tpnew"]=GetTpExp("Tp");
		XBtnExp["tplast"]=XBtnExp["tpnew"]-XBtnExp["tpold"];
		XBtnExp["tpsession"]=XBtnExp["tpsession"]+XBtnExp["tplast"];
	end
	if XBtnExp["xpnew"]~=GetPlayerExp("player") then
		if XBtnExp["xpnew"]~=0 then XBtnExp["xpold"]=XBtnExp["xpnew"];
		else XBtnExp["xpold"]=GetPlayerExp("player"); end
		XBtnExp["xpnew"]=GetPlayerExp("player");
		XBtnExp["xplast"]=XBtnExp["xpnew"]-XBtnExp["xpold"];
		XBtnExp["xpold"]=XBtnExp["xpnew"];
		XBtnExp["xpsession"]=XBtnExp["xpsession"]+XBtnExp["xplast"];
		XBtnExp["xpmax"]=GetPlayerMaxExp("player");
		XBtnExp["xpleft"]=XBtnExp["xpmax"]-XBtnExp["xpnew"];
		if XBtnExp["xplast"]~=0 then XBtnExp["xprepeats"]=math.ceil(XBtnExp["xpleft"]/XBtnExp["xplast"]);
		else XBtnExp["xprepeats"]="-.-"; end
	end
--	if (event=="UNIT_CLASS_CHANGED") then XBtnExp["xpnew"]=0; XBtnExp["tpnew"]=0; end
--	Output
	local usrtxt={[1]=XBar["ExpV1"],[2]=XBar["ExpV2"]};
	local output="";
	for i=1,2 do
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[XP%]",XBar_SetDecimals(XBtnExp["xpnew"]));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[TP%]",XBar_SetDecimals(XBtnExp["tpnew"]));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[XPNEED%]",XBar_SetDecimals(XBtnExp["xpmax"]-XBtnExp["xpnew"]));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[XPFULL%]",XBar_SetDecimals(XBtnExp["xpmax"]));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[PERCENT%]",string.format("%.2f",(XBtnExp["xpnew"]/XBtnExp["xpmax"])*100));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[PERCENTNEED%]",string.format("%.2f",((XBtnExp["xpmax"]-XBtnExp["xpnew"])/XBtnExp["xpmax"])*100));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[XPBONUS%]",XBar_SetDecimals(XBtnExp["xpbonus"]));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[TPBONUS%]",XBar_SetDecimals(XBtnExp["tpbonus"]));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[XPDEPT%]",XBar_SetDecimals(XBtnExp["xpdebt"]));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[TPDEPT%]",XBar_SetDecimals(XBtnExp["tpdebt"]));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[XPLAST%]",XBar_SetDecimals(XBtnExp["xplast"]));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[TPLAST%]",XBar_SetDecimals(XBtnExp["tplast"]));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[XPSESSION%]",XBar_SetDecimals(XBtnExp["xpsession"]));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[TPSESSION%]",XBar_SetDecimals(XBtnExp["tpsession"]));
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[XPREPEATS%]",XBar_SetDecimals(XBtnExp["xprepeats"]));
	end
	if (XBar["ExpT1"]==true) then output=usrtxt[1]; end
	if (XBar["ExpT2"]==true) then
		if (XBar["ExpT1"]==true) then output=output.."\n"..usrtxt[2]; else output=usrtxt[2]; end
	end
	XBarExp_F_Text:SetText(output);
--	EoL
end
